package com.mzj.saas.sdk.refund;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.mzj.saas.sdk.pay.callback.WxRefundCallback;
import com.mzj.saas.sdk.refund.vo.WxRefund;
import com.mzj.saas.sdk.refund.wx.WxRefundRequest;

/**
 * 退款测试用例
 * @author hp
 *
 */
public class Test {

	public static void main(String[] args) {

		//微信退款
		String refundApi = "https://api.mch.weixin.qq.com/secapi/pay/refund";
		//查询退款
		String refundquery = "https://api.mch.weixin.qq.com/pay/refundquery";
		//WxRefund wxRefund = new WxRefund(appId, mchId, mchApiKey, transactionId, outTradeNo, outRefundNo, totalFee, refundFee, notifyUrl, mchCertificate,refundApi,refundquery);
		WxRefund wxRefund = new WxRefund();
		WxRefundRequest wxRefundRequest = new WxRefundRequest(wxRefund);
		Map<String, String> wxRefundResult = wxRefundRequest.refund();
		if ("SUCCESS".equals(wxRefundResult.get("return_code")) && "SUCCESS".equals(wxRefundResult.get("result_code"))) {
			//微信退款申请已提交 成功;通过回调来确认是否退款成功
			wxRefundResult.get("transaction_id");
			wxRefundResult.get("refund_id");
		}else {
			//失败
			String errCode = wxRefundResult.get("err_code");
			String errCodeDes = wxRefundResult.get("err_code_des");
		}
		
		//微信退款通知
		String mchApiKey=null;
		HttpServletRequest request=null;
		HttpServletResponse response = null;
		WxRefundCallback wxRefundCallback = new WxRefundCallback(mchApiKey,request,response);
		Map<String, String> callbackResult = wxRefundCallback.verifyResult();
		
		if(callbackResult == null) {
			//系统异常
		}
		
		if("SUCCESS".equals(callbackResult.get("refund_status"))) {
			//退款成功，相关业务处理
		}
		
		
		//查询退款状态
		Map<String, String> refundQueryMap =  wxRefundRequest.refundquery();
		if(refundQueryMap != null) {
			if ("FAIL".equals(refundQueryMap.get("result_code"))) {
				refundQueryMap.get("err_code");
				refundQueryMap.get("err_code_des");
			}
			//refund_status_$n   $n为下标，从0开始编号。
			//比如 一个订单，可以最多退款50次，比如说商户一个订单退款了21次，去查询的时候，如果传transaction_id查询，这里就会返回20
			
			if ("SUCCESS".equals(refundQueryMap.get("return_code")) && "SUCCESS".equals(refundQueryMap.get("result_code"))) {
				if("SUCCESS".equals(refundQueryMap.get("refund_status_0"))){
					//退款成功
				}
				if("REFUNDCLOSE".equals(refundQueryMap.get("refund_status_0"))){
					//交易关闭
				}
				
				if("PROCESSING".equals(refundQueryMap.get("refund_status_0"))){
					//交易处理中
				}
				if("CHANGE".equals(refundQueryMap.get("refund_status_0"))){
					//退款异常，退款到银行发现用户的卡作废或者冻结了，导致原路退款银行卡失败，可前往商户平台（pay.weixin.qq.com）-交易中心，手动处理此笔退款。
				}
			}
		}
		
		
		
		
		
		
	}

}
